package com.ncut.common;


import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class Histogram extends ApplicationFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Histogram(String s) {
		super(s);
		JPanel jpanel = createDemoPanel();
		jpanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(jpanel);
	}
	private static CategoryDataset createDataset() {

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(300,"北京","苹果");
		dataset.addValue(200,"北京","梨子");
		dataset.addValue(500,"北京","葡萄");
		dataset.addValue(340,"北京","芒果");
		dataset.addValue(280,"北京","荔枝");
		return dataset;
	}
	private static JFreeChart createChart(CategoryDataset categorydataset) {
		
		
		JFreeChart jfreechart = ChartFactory.createBarChart("水果销量图统计",
				"水果", "销量", categorydataset, PlotOrientation.VERTICAL,
				true, true, false);
		
		/**
		 *解决中英文字体乱码
		 * title(标题)
		 * legend(图释)
		 * plot(图表主体)。
		 */
		Font font = new Font("SimSun",10,20);
		
		jfreechart.getTitle().setFont(font);
		
		CategoryPlot plot = (CategoryPlot)jfreechart.getPlot();
		
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setTickLabelFont(font);// 设置X轴坐标上的字体
		domainAxis.setLabelFont(font);//设置X轴上的标题的字体
        
		ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)
		valueAxis.setTickLabelFont(font);//设置y轴坐标上的字体
		valueAxis.setLabelFont(font);//设置y轴上标题的字体
		
		jfreechart.getLegend().setItemFont(font);
		return jfreechart;
	}
	
	public static JPanel createDemoPanel() {
		JFreeChart jfreechart = createChart(createDataset());
		return new ChartPanel(jfreechart);
	}

	public static void main(String args[]) {
		Histogram barchartdemo1= new Histogram("Bar Chart Demo");
		barchartdemo1.pack();
		RefineryUtilities.centerFrameOnScreen(barchartdemo1);
		barchartdemo1.setVisible(true);
	}
	
}
